﻿// =================================================================== 
// 项目说明
//====================================================================
// 文件： ERP_Type.cs
// 创建时间：2016/11/22
// 负责人：
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using CRMMis.Model;
using CRMMis.IDAL;


namespace MSSQLDAL
{
    /// <summary>
    /// 数据层实例化接口类  .
    /// </summary>
    public partial class ERP_TypeDAL : IERP_TypeDAL
    {
		//此处写查询数据的sql语句，


        //if QueryDataString==""，DAL层的GetList，GetRecordCount等函数将直接存取ERP_Type表


        //if QueryDataString!=""，则DAL层的GetList，GetRecordCount等函数将存取该SQL语句，取代表
		//注意：所写sql语句执行结果的字段必须是唯一的

		private const string QueryDataString = "";

        public DataSet GetListSub(string sWhere, string sOder)
        {
            string sqlStr = "select Type_Id,Type_Name ,( case when Type_Id  in (select PARENT_ID from ERP_Type) then 1 else 0 end) as leaf  from " + TableString;
            if (sWhere.Length > 0)
                sqlStr += " where " + sWhere;
            if (sOder.Length > 0)
                sqlStr += " order by " + sOder;
            return SqlHelper.ExecuteDataset(Conn.SqlConnStr, CommandType.Text, sqlStr);
        }

        public string GetNewParentId(string parentId)
        {
            string sSQL = "select '" + parentId + "' + substring(snewid,len('" + parentId + "')+1,2) "
                + " from ("
                + " select Replicate('0',case when (len('" + parentId + "__')-len(cast( MAX(INNER_NO)+1 as varchar(10)))) <0 then 0 else (len('" + parentId + "__')-len(cast( MAX(INNER_NO)+1 as varchar(10)))) end ) + cast( MAX(INNER_NO)+1 as varchar(10)) snewid "
                + " FROM ERP_Type WHERE lTRIM(INNER_NO) LIKE '" + parentId + "__'"
                + " )t";
            return SqlHelper.ExecuteScalar(Conn.SqlConnStr, CommandType.Text, sSQL).ToString();
        }
    }
}
